package com.hidtechs.popfax.view;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.hidtechs.popfax.controller.Constants;
import com.hidtechs.popfax.controller.Requestor;
import com.hidtechs.popfax.iconifiedList.ContactsTextListAdapter;
import com.hidtechs.popfax.iconifiedList.IconifiedTextListAdapter;
import com.hidtechs.popfax.xml.ParsedContactsDataSet;

public class Contacts  extends ListActivity implements Runnable{
    /** Called when the activity is first created. */
	
	Character class_type='c';
	protected Handler handler;
	protected HttpURLConnection conn=null;
	private ProgressDialog progressDialog;
	public HashMap<String,String> parameterValues;
	protected ArrayList arrayOfObjectOfParsedData;
	protected ArrayList arrayListretrieve[];
	String userName_shared_prefernece;
	String passWord_shared_prefernece;
	ContactsTextListAdapter itlaContacts;




public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	contactsData();
	
}

private void contactsData()
{

	progressDialog = ProgressDialog.show(this,Constants.DIALOG_WAIT_MSG,Constants.CONTACTS_DATA_RETRIEVING_MSG, true,false);
	itlaContacts = new ContactsTextListAdapter(this);

	Thread thread = new Thread(this);
	thread.start(); 
	
	loginHandlerMethod();


}

public void run() {
	System.out.println("In Run");

	setMapValues();
	Requestor requestorForXMLParsing= new Requestor();
	arrayOfObjectOfParsedData=requestorForXMLParsing.requestData(class_type,parameterValues);

	handler.sendEmptyMessage(0);		
}


public void loginHandlerMethod()
{
	handler = new Handler() {

		public void handleMessage(Message msg) {
			progressDialog.dismiss();

			System.out.println("Handler");
			System.out.println(arrayOfObjectOfParsedData.size());
			arrayListretrieve = new ArrayList[arrayOfObjectOfParsedData.size()];
	        ArrayList<String> headingArrayList = new ArrayList<String>();
	        headingArrayList.add("NAME");
	        headingArrayList.add("FAX");
	        headingArrayList.add("GSM");
	        itlaContacts.addItem(headingArrayList);
	        System.out.println("size="+arrayOfObjectOfParsedData.size());
	        for(int i=0;i<arrayOfObjectOfParsedData.size();i++)
	        {
	        	arrayListretrieve[i]= new ArrayList();
	        	arrayListretrieve[i].add(((ParsedContactsDataSet)(arrayOfObjectOfParsedData.get(i))).getName());
	        	arrayListretrieve[i].add(((ParsedContactsDataSet)(arrayOfObjectOfParsedData.get(i))).getNumber());
	        	arrayListretrieve[i].add(((ParsedContactsDataSet)(arrayOfObjectOfParsedData.get(i))).getGsmnumber());
	        	arrayListretrieve[i].add(((ParsedContactsDataSet)(arrayOfObjectOfParsedData.get(i))).getId());
	        	itlaContacts.addItem(arrayListretrieve[i]);
	        }
	        
	        setListAdapter(itlaContacts);
	 
		}

	};

}

void setMapValues() {

	loadPreference(); 
	parameterValues = new HashMap();
	parameterValues.put(Constants.URL_ACTION, Constants.CONTACTS_ACTION);
	parameterValues.put(Constants.URL_USERNAME,userName_shared_prefernece);
	parameterValues.put(Constants.URL_PASSWORD,passWord_shared_prefernece);

}



protected void loadPreference()
{
	int mode = Activity.MODE_PRIVATE;
	SharedPreferences mySharedPreferences = getSharedPreferences("preferences", mode);
	userName_shared_prefernece= mySharedPreferences.getString("USER_NAME","UserName");
	System.out.println(userName_shared_prefernece);
	passWord_shared_prefernece = mySharedPreferences.getString("PASS_WORD","Password");
	System.out.println(passWord_shared_prefernece);
}

}
